<!-- 发票列表 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="客户名称">
                            <a-input v-model="customerName" placeholder="请输入客户名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="开票内容">
                            <a-input v-model="receiptName" placeholder="请输入开票内容" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="发票类型">
                            <a-select v-model="receiptType" placeholder="请选择" allow-clear>
                                <a-select-option v-for="item in receiptTypeList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="state" placeholder="请选择" allow-clear>
                                <a-select-option v-for="item in stateList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="申请时间">
                            <a-range-picker @change="onDateChange" allow-clear/>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <!-- <a-button style="margin-left: 20px" type='' icon="download" @click="download">导出</a-button> -->
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}"  size="small" :scroll="{ x: 1500, }">
            <div slot="receiptType" slot-scope="receiptType">
                <div v-for="(item,i) in receiptTypeList" :key="item.id">
                    <a-tag v-if="item.id == receiptType" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="state" slot-scope="state">
                <div v-for="(item,i) in stateList" :key="item.id">
                    <a-tag v-if="item.id == state" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="receiptName" slot-scope="receiptName">
                <ellipsis :length="6" tooltip>{{ receiptName == '' || !receiptName ? '-' : receiptName }}</ellipsis>
            </div>
            <div slot="remark" slot-scope="remark">
                <ellipsis :length="12" tooltip>{{ remark == '' || !remark ? '-' : remark }}</ellipsis>
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button v-if="record.state == 1 && hasPower('refuseReceipt')" type="link" @click="success(record)">通过</a-button>
                    <a-button v-if="record.state == 1 && hasPower('refuseReceipt')" type="link" @click="refuse(record)">拒绝</a-button>
                    <a-button v-if="record.state == 2 && hasPower('makeReceipt')" type="link" @click="makes(record)">开票</a-button>
                    <a-button v-if="record.state == 3" type="link" @click="mail(record)">邮寄</a-button>
                    <a-button type="link" @click="detail(record)">详情</a-button>
                </div>
            </div>
        </a-table>

        <!-- 拒绝原因 -->
        <a-modal
            title="添加拒绝原因"
            :visible="refuseVisible"
            @ok="refuseHandleOk"
            @cancel="refuseVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="拒绝原因" help="拒绝开票，请向客户说明拒绝原因">
                        <a-textarea :rows="4" v-model="refuseReason" allow-clear placeholder="请输入拒绝原因" :maxLength="100"/>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 开票设置 -->
        <a-modal
            title="开票设置"
            :visible="makeVisible"
            @ok="makeHandleOk"
            @cancel="makeVisible = false"
            >
            <div>
                <a-form-model :model="makeConfig" ref="ruleForm" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-model-item label="发票查询码">
                        <a-input v-model="makeConfig.queryCode" allow-clear placeholder="请输入发票查询码" />
                    </a-form-model-item>
                    <a-form-model-item label="发票快照" prop="receiptUrl">
                        <a-upload
                            name="file"
                            @change="handleChangeInvoice"
                            :before-upload="beforeUpload"
                            :fileList = invoiceFileList
                            accept=".pdf,.jpg,.png,.jpeg"
                            :remove="()=>{invoiceFileList = []}"
                        >
                            <a-button type="primary"> 上传 </a-button>
                            <p class="ant-upload-text" style="margin-top: 10px">
                                仅支持上传PDF、jpg、png、jpeg格式，文件大小最多10M
                            </p>
                        </a-upload>
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>

        <!-- 邮递信息 -->
        <a-modal
            title="邮寄设置"
            :visible="mailVisible"
            @ok="mailHandleOk"
            @cancel="mailVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="快递公司" >
                        <a-input v-model="postCompany" allow-clear placeholder="请输入快递公司" />
                    </a-form-item>
                    <a-form-item label="快递单号" help="快递公司和快递单号用于客户查询邮寄信息；">
                        <a-input v-model="postNumber" allow-clear placeholder="请输入快递单号" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { getReceiptList , refuseReceipt , adoptReceipt , makeReceipt , mailReceipt } from '@/api/services/invoice';
import { fileUpload } from '@/api/system';
import { Ellipsis } from '@/components';
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            dataList: [],
            isload: false,
            page: 1,
            size: 10,
            total: 0,
            columns,
            startTime: '',
            endTime: '',
            customerName: '', // 客户名称
            receiptName: '', // 开票内容
            receiptType: undefined, // 发票类型
            receiptTypeList: [
                { id: 1, name: '普通发票'},
                { id: 2, name: '专业发票'},
            ],
            state: undefined,
            stateList: [
                { id: 1, name: '待审核'},
                { id: 2, name: '待开票'}, 
                { id: 3, name: '待邮寄'}, 
                { id: 4, name: '已邮寄'}, 
                { id: 8, name: '已拒绝'}, 
                { id: 9, name: '已取消'}, 
            ],
            refuseVisible: false,
            refuseReason: '',
            makeVisible: false,
            invoiceCode: '', // 发票查询码
            invoiceFile: {},
            invoiceFileList: [],
            mailVisible: false, 
            invoice: {
                id: ''
            },
            makeConfig: {
                queryCode: '', // 查询码
                receiptUrl: '',
                receiptId: '', // 发票ID
            }, // 开票内容
            rules: {
                contractUrl: [{ required: true, message: '请上传发票快照', trigger: 'blur' }],
            },
            postCompany: '', // 快递公司
            postNumber: '', // 快递单号
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = `${dateStr[0]}`
                this.endTime = `${dateStr[1]}`
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'customerName': _this.customerName, 
                'receiptName': _this.receiptName,
                'receiptType': _this.receiptType,
                'state': _this.state,
                'pageSize': _this.size,
                'pageIndex': _this.page,
            }
            getReceiptList(params)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 通过审核
        success(item){
            let _this = this
            _this.$confirm({
                title: '温馨提示',
                content: '审核通过后，将不可取消通过状态，请谨慎操作；',
                onOk() {
                    adoptReceipt(item.id)
                    .then(res => {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '操作成功',
                                duration: 8
                            })
                            _this.getData();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
        // 拒绝审核
        refuse(item){
            this.invoice = JSON.parse(JSON.stringify(item));
            this.refuseVisible = true;
        },
        refuseHandleOk(){
            let _this = this
            console.log(_this.refuseReason)
            const params = {
                'receiptId': _this.invoice.id,
                'refuseReason': _this.refuseReason,
            }
            refuseReceipt(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '操作成功',
                        duration: 8
                    })
                    _this.refuseVisible = false;
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 开票
        makes(item){
            // this.invoice = JSON.parse(JSON.stringify(item));
            this.makeConfig.receiptId = item.id;
            this.makeConfig.queryCode = ''
            this.makeConfig.receiptUrl = ''
            this.makeVisible = true;
        },
        beforeUpload(file) {
            return false;
        },
        handleChangeInvoice({file,fileList}){
            let _this = this
            const status = file.status;
            if(file.status === 'removed'){
                this.invoiceFileList = []
                return
            }
            this.invoiceFileList = fileList
            const formData = new FormData();
            formData.append('multipartFile',file)
            fileUpload(formData)
            .then(res=>{
                if(res.success){
                    _this.makeConfig.receiptUrl = res.data
                    _this.$refs.ruleForm.clearValidate()
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        makeHandleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    makeReceipt(_this.makeConfig).then(res=>{
                        if(res.success){
                            _this.makeVisible = false;
                            _this.getData();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '开票成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 邮寄
        mail(item){
            this.invoice = JSON.parse(JSON.stringify(item));
            this.mailVisible = true;
        },
        mailHandleOk(){
            let _this = this
            if(_this.postCompany == ''){
                _this.$message.warning('请输入快递公司名称');
                return;
            }
            if(_this.postNumber == ''){
                _this.$message.warning('请输入快递单号');
                return;
            }
            const params = {
                'receiptId': _this.invoice.id,
                'postCompany': _this.postCompany,
                'postNumber': _this.postNumber,
            }
            mailReceipt(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '邮寄成功',
                        duration: 8
                    })
                    _this.mailVisible = false;
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 详情
        detail(item){
            this.$router.push({ path: '/servicesInvoice/invoiceDetail', query: { 'receiptId': item.id }});
        },
    }
}

</script>
<style scoped>

</style>